package com.yxjx.duoxue.course;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.HomePageActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.utils.UIHelper;

public class CitySwitchActivity extends BaseActionBarActivity {
	
	public static final int REQ_CODE_GET_CITY_BY_SEARCH = 1;
	
	int mLastSearchTextLength = 0;
	
	private ActionBar mActionbar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_switch_city);
		customizeActionBarTitle("切换城市");
		
		mActionbar = getSupportActionBar();
		
		mSearchBox = (EditText) findViewById(R.id.search_box);
		
		mList = (ListView) findViewById(R.id.list);
		mCityListAdapter = new CityListAdapter();
		mList.setAdapter(mCityListAdapter);

		mSearchList = (ListView) findViewById(R.id.search_list);
		mSearchCityListAdapter = new SearchCityListAdapter(mCityListAdapter.getCityNames(), "");
		mSearchList.setAdapter(mSearchCityListAdapter);
		
		mSearchBox.setOnClickListener(mClickListener);
		mSearchBox.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				if (mLastSearchTextLength == 0 && arg0.toString().length() > 0) {
					setSearchMode(true);
				} else if (mLastSearchTextLength > 0 && arg0.toString().length() == 0) {
					setSearchMode(false);
				}
				Log.d("CMM", "onTextChanged arg0 =" + arg0 + ", arg1=" + arg1 +
						",arg2=" + arg2 + ",arg3" + arg3);
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				mLastSearchTextLength = arg0.toString().length();
				Log.d("CMM", "beforeTextChanged arg0 =" + arg0 + ", arg1=" + arg1 +
						",arg2=" + arg2 + ",arg3" + arg3);
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				Log.d("CMM", "afterTextChanged arg0 =" + arg0);
				mSearchCityListAdapter.setFilterString(arg0.toString());
				mSearchCityListAdapter.notifyDataSetChanged();
			}
		});
		
		mList.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				mSearchBox.clearFocus();
				return false;
			}
		});
		
	}
	
	private void setSearchMode(boolean search) {
		mSearchList.setVisibility(search ? View.VISIBLE : View.INVISIBLE);
		mList.setVisibility(search ? View.INVISIBLE : View.VISIBLE);
	}
	
	class CityListAdapter extends BaseAdapter {
		
		private static final int TYPE_CATEGORY = 0;
		private static final int TYPE_CITY = 1;
		private static final int TYPE_COUNT = 2;
		
		String[][] cities = {
				{"GPS定位城市","北京"},
				{"热门城市","北京","上海","广州","深圳","武汉","南京","长沙","苏州","杭州","天津",
					"海口","开封","葫芦岛","赤峰","荆州","常德","连云港","泰州","台州","衢州"}
		};
		private ArrayList<String> mValues;
		private ArrayList<Integer> mTypes;
		
		public CityListAdapter() {
			mValues = new ArrayList<String>();
			mTypes = new ArrayList<Integer>();
			for (int i = 0; i < cities.length; i++) {
				for (int j = 0; j < cities[i].length; j++) {
					mValues.add(cities[i][j]);
					mTypes.add(j == 0 ? TYPE_CATEGORY : TYPE_CITY);
				}
			}
		}
		
		public ArrayList<String> getCityNames() {
			ArrayList<String> ret = new ArrayList<String>();
			for (int i = 0; i < mValues.size(); i++) {
				if (mTypes.get(i) == TYPE_CITY) {
					ret.add(mValues.get(i));
				}
			}
			return ret;
		}
		
		@Override
		public int getViewTypeCount() {
			return TYPE_COUNT;
		}
		
		@Override
		public int getItemViewType(int position) {
			return mTypes.get(position);
		}

		@Override
		public int getCount() {
			return mValues.size();
		}
		
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (getItemViewType(arg0) == TYPE_CATEGORY) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_city_category, null);
				} else {
					ret = arg1;
				}
			} else if (getItemViewType(arg0) == TYPE_CITY) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_city_name, null);
				} else {
					ret = arg1;
				}
			}
			ret.setOnClickListener(mClickListener);
			UIHelper.setText(ret, R.id.text, mValues.get(arg0));
			return ret;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}
	}
	
	class SearchCityListAdapter extends BaseAdapter {
		private ArrayList<String> mVaules;
		private String mFilterString;
		private ArrayList<String> mFilterValues;
		
		public SearchCityListAdapter(ArrayList<String> cityNames, String filterString) {
			mVaules = cityNames;
			mFilterString = filterString;
		}
		
		@Override
		public int getCount() {
			mFilterValues = new ArrayList<String>();
			for (String item : mVaules) {
				if (item.contains(mFilterString)) {
					mFilterValues.add(item);
				}
			}
			
			if (mFilterValues.size() == 0) {
				return 1;
			} else {
				return mFilterValues.size();
			}
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret;
			if (arg1 == null) {
				ret = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_city_name, null);
			} else {
				ret = arg1;
			}
			if (mFilterValues.size() == 0) {
				UIHelper.setText(ret, R.id.text, "未找到匹配的城市");
			} else {
				UIHelper.setText(ret, R.id.text, mFilterValues.get(arg0));
				ret.setOnClickListener(mClickListener);
			}
			return ret;
		}
		
		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		public ArrayList<String> getVaules() {
			return mVaules;
		}

		public void setVaules(ArrayList<String> mVaules) {
			this.mVaules = mVaules;
		}

		public String getFilterString() {
			return mFilterString;
		}

		public void setFilterString(String mFilterString) {
			this.mFilterString = mFilterString;
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if (arg0 == REQ_CODE_GET_CITY_BY_SEARCH) {
			switch (arg1) {
			case RESULT_OK:
				setResult(RESULT_OK, arg2);
				finish();
				break;
			default:
				break;
			}
		}
		super.onActivityResult(arg0, arg1, arg2);
	}
	
	private View.OnClickListener mClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.city_item:
				Intent intent = getIntent();
				String text = UIHelper.getText(arg0, R.id.text);
				if (!text.equals(getResources().getString(R.string.city_not_found))) {
					intent.putExtra(HomePageActivity.KEY_CITY_NAME, text);
					setResult(RESULT_OK, intent);
					finish();
				}
				break;
			case R.id.search_box:
//				intent = new Intent(getApplicationContext(), SearchCityListActivity.class);
//				startActivityForResult(intent, REQ_CODE_GET_CITY_BY_SEARCH);
				break;
			default:
				break;
			}
		}
	};

	private SearchCityListAdapter mSearchCityListAdapter;

	private CityListAdapter mCityListAdapter;

	private ListView mList;

	private ListView mSearchList;

	private EditText mSearchBox;

}
